<template>
  <div class="notification-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>通知列表</span>
          <el-button v-if="hasPublishPermission" type="primary" @click="$router.push('/notification/publish')">发布通知</el-button>
        </div>
      </template>
      
      <!-- 搜索区域 -->
      <el-form :inline="true" :model="queryParams" @submit.prevent>
        <el-form-item label="通知标题">
          <el-input v-model="queryParams.title" placeholder="请输入通知标题" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="通知类型">
          <el-select v-model="queryParams.type" placeholder="请选择通知类型" clearable>
            <el-option label="普通通知" :value="0" />
            <el-option label="重要通知" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      
      <!-- 表格区域 -->
      <el-table v-loading="loading" :data="notificationList" style="width: 100%">
        <el-table-column prop="title" label="通知标题" min-width="200" show-overflow-tooltip />
        <el-table-column prop="type" label="通知类型" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.type === 1 ? 'danger' : ''">
              {{ scope.row.type === 1 ? '重要通知' : '普通通知' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="publishUserName" label="发布人" width="120" />
        <el-table-column prop="publishTime" label="发布时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="viewNotification(scope.row)">查看</el-button>
            <el-button v-if="hasEditPermission" type="primary" link @click="editNotification(scope.row)">编辑</el-button>
            <el-button v-if="hasDeletePermission" type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页区域 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    
    <!-- 查看通知对话框 -->
    <el-dialog v-model="viewDialog" title="查看通知" width="700px" destroy-on-close>
      <div class="notification-view">
        <h2 class="notification-title">{{ currentNotification.title }}</h2>
        <div class="notification-meta">
          <span>发布人: {{ currentNotification.publishUserName }}</span>
          <span>发布时间: {{ currentNotification.publishTime }}</span>
          <el-tag :type="currentNotification.type === 1 ? 'danger' : ''">
            {{ currentNotification.type === 1 ? '重要通知' : '普通通知' }}
          </el-tag>
        </div>
        <div class="notification-content" v-html="currentNotification.content"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getNotificationList, markAsRead, markAllAsRead, deleteNotification } from '@/api/notification'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 权限判断
const hasPublishPermission = computed(() => userStore.hasPermission('notification:publish'))
const hasEditPermission = computed(() => userStore.hasPermission('notification:edit'))
const hasDeletePermission = computed(() => userStore.hasPermission('notification:delete'))

// 查询参数
const queryParams = reactive({
  title: '',
  type: undefined,
  pageNum: 1,
  pageSize: 10
})

const loading = ref(false)
const notificationList = ref([])
const total = ref(0)

// 查看通知相关
const viewDialog = ref(false)
const currentNotification = ref({})

// 查询通知列表
const getList = async () => {
  loading.value = true
  try {
    const { data } = await getNotificationList(queryParams)
    notificationList.value = data.list
    total.value = data.total
  } catch (error) {
    console.error('获取通知列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 查询
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.title = ''
  queryParams.type = undefined
  handleQuery()
}

// 分页大小变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  getList()
}

// 页码变化
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  getList()
}

// 查看通知
const viewNotification = (row) => {
  currentNotification.value = { ...row }
  viewDialog.value = true
}

// 编辑通知
const editNotification = (row) => {
  const { id } = row
  router.push(`/notification/publish?id=${id}`)
}

// 删除通知
const handleDelete = (row) => {
  ElMessageBox.confirm(`确认删除通知"${row.title}"?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteNotification(row.id)
      ElMessage.success('删除成功')
      getList()
    } catch (error) {
      console.error('删除通知失败:', error)
    }
  }).catch(() => {})
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.notification-list {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .pagination {
    margin-top: 20px;
    text-align: right;
  }
  
  .notification-view {
    .notification-title {
      text-align: center;
      margin-bottom: 20px;
    }
    
    .notification-meta {
      display: flex;
      justify-content: space-between;
      margin-bottom: 20px;
      color: #909399;
      font-size: 14px;
    }
    
    .notification-content {
      line-height: 1.8;
      padding: 20px;
      background-color: #f8f8f8;
      border-radius: 4px;
    }
  }
}
</style> 